﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Net.Mail;

namespace it_security_project.App_Code.BusinessLogicLayer
{
    public class Member : BusinessObjectBase
    {
        private Roles _Role = Roles.Member;
        private string _Username;
        private string _Password;
        private string _RepeatPassword;
        private string _EmailAdress;
        private string _SecurityQuestion;
        private string _SecurityQuestionAnswer;

        public int MemberID
        {
            get;
            set;
        }

        public Roles Role
        {
            get { return this._Role; }
            set { _Role = value; }
        }

        public string Username
        {
            get { return this._Username; }
            set
            {
                this.ValidationErrors.Remove("Username");

                if (String.IsNullOrWhiteSpace(value))
                {
                    this.ValidationErrors.Add("Username", "You have to write a Username.");
                }
                else if (value.Length > 30)
                {
                    this.ValidationErrors.Add("Username", "The Username can be max 30 characters.");
                }
                else if (value != Regex.Replace(value, "<.*?>", string.Empty))
                {
                    this.ValidationErrors.Add("Username", "The username contains characters that are not allowed");
                }
                this._Username = value != null ? value.Trim() : null;
            }
        }

        public string Password
        {
            get { return this._Password; }
            set
            {
                this.ValidationErrors.Remove("Password");

                if (String.IsNullOrWhiteSpace(value))
                {
                    this.ValidationErrors.Add("Password", "You have to write a Username.");
                }
                else if (value.Length > 50)
                {
                    this.ValidationErrors.Add("Password", "The Password can be max 50 characters.");
                }

                this._Password = value != null ? value.Trim() : null;
            }
        }

        public string RepeatPassword
        {
            get { return this._RepeatPassword; }
            set
            {
                this.ValidationErrors.Remove("RepeatPassword");

                if (String.IsNullOrWhiteSpace(value))
                {
                    this.ValidationErrors.Add("RepeatPassword", "You have to write a Username.");
                }
                else if (value.Length > 50 || value != this._Password)
                {
                    this.ValidationErrors.Add("RepeatPassword", "The Repeated Password need to match your password.");
                }
                this._RepeatPassword = value != null ? value.Trim() : null;
            }
        }

        public string EmailAdress
        {
            get { return this._EmailAdress; }
            set
            {
                //Regex regex = new Regex("@^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\[a-zA-Z0-9-.]+$");
                this.ValidationErrors.Remove("EmailAdress");
                try 
	            {	        
		            MailAddress mail = new MailAddress(value);
	            }
	            catch
	            {
		            ValidationErrors.Add("EmailAdress","Email is invalid!");
	            }
                
                if (String.IsNullOrWhiteSpace(value))
                {
                    this.ValidationErrors.Add("EmailAdress", "An email must be provided.");
                }                
                else if (value.Length > 50)
                {
                    this.ValidationErrors.Add("EmailAdress", "The EmailAdress can be max 50 characters.");
                }

                this._EmailAdress = value != null ? value.Trim() : null;
            }
        }

        public string SecurityQuestion
        {
            get { return this._SecurityQuestion; }
            set
            {
                this.ValidationErrors.Remove("SecurityQuestion");

                if (String.IsNullOrWhiteSpace(value))
                {
                    this.ValidationErrors.Add("SecurityQuestion", "You have to write a SecurityQuestion.");
                }
                else if (value.Length > 100)
                {
                    this.ValidationErrors.Add("SecurityQuestion", "The SecurityQuestion can be max 100 characters.");
                }
                this._SecurityQuestion = value != null ? value.Trim() : null;
            }
        }

        public string SecurityQuestionAnswer
        {
            get { return this._SecurityQuestionAnswer; }
            set
            {
                this.ValidationErrors.Remove("SecurityQuestionAnswer");

                if (String.IsNullOrWhiteSpace(value))
                {
                    this.ValidationErrors.Add("SecurityQuestionAnswer", "You have to write an answer to your security question.");
                }
                else if (value.Length > 100)
                {
                    this.ValidationErrors.Add("SecurityQuestionAnswer", "The security question answer can be max 100 characters.");
                }
                this._SecurityQuestionAnswer = value != null ? value.Trim() : null;
            }
        }

        public Member()
        {
        }

        public Member(string username, string password, string repeatPassword, string emailAdress, string securityQuestion, string securityQuestionAnswer)
        {
            Username = username;
            Password = password;
            RepeatPassword = repeatPassword;
            EmailAdress = emailAdress;
            SecurityQuestion = securityQuestion;
            SecurityQuestionAnswer = securityQuestionAnswer;
        }

        public Member(int memberID, string username, string password, string repeatPassword, string emailAdress, string securityQuestion, string securityQuestionAnswer)
        {
            MemberID = memberID;
            Username = username;
            Password = password;
            RepeatPassword = repeatPassword;
            EmailAdress = emailAdress;
            SecurityQuestion = securityQuestion;
            SecurityQuestionAnswer = securityQuestionAnswer;
        }
    }
}